<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24" class="top">
        <el-form :model="searchParams" ref="searchParams" :inline="true">
          <el-form-item label="用户名" prop="unit">
            <el-input class="input" v-model="searchParams.username" maxlength='20' placeholder="请输入" clearable
              show-word-limit></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleSearch" type="primary" icon="el-icon-search"
              style="margin-left:40px">搜索</el-button>
          </el-form-item>
        </el-form>
        <el-form :model="searchParams" ref="searchParams" :inline="true" style="margin:10px 0">
          <el-button @click="operateAction('create', {})" type="primary" plain size='small'>新增</el-button>
          <el-button @click="operateAction('delete', {})" type="info" plain size='small'>禁用</el-button>
        </el-form>
        <el-table :data="dataList" size='medium' ref="multipleTable" @selection-change="handleSelectionChange" border>
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="id" width="50" show-overflow-tooltip></el-table-column>
          <el-table-column prop="username" label="用户名" show-overflow-tooltip></el-table-column>
          <el-table-column prop="nickname" label="昵称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="roleModel.name" label="角色" show-overflow-tooltip></el-table-column>
          <el-table-column prop="status" label="状态" show-overflow-tooltip>
            <template slot-scope="{row, index}">
              {{ formData.dicts.status[row.status] }}
            </template>
          </el-table-column>
          <el-table-column prop="sort" label="顺序" show-overflow-tooltip></el-table-column>
          <el-table-column prop="redact" label="操作" fixed="right" width="120">
            <template slot-scope="{ row, index }">
              <el-button type="text" @click="operateAction('update', row)" size='medium'>修改</el-button>
              <el-button type="text" @click="operateAction('detail', row)" size='medium'>详情</el-button>
              <el-button type="text" @click="operateAction('resetPassword', row)" size='medium' v-if="passRole(['admin', 'lessee'])">重置密码</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination style="background: #ffffff; height: 50px" v-show="totalSize > 0" :total="totalSize"
          :page.sync="page" :limit.sync="size" @pagination="getDataList" />
      </el-col>
    </el-row>
    <resetPwd ref="resetPwd" @confirm='getDataList'></resetPwd>
  </div>
</template>

<script>
import { formData } from './data'
import { findAdminList, deleteAdmins } from '@/api/system/admin.js'
import { findRoleList } from '@/api/system/role.js'
import resetPwd from '@/views/system/userList/components/resetPwd.vue'
export default {
  components: { resetPwd },
  data() {
    return {
      page: 1,
      size: 10,
      totalSize: 0,
      dataList: [],
      searchParams: {},
      multipleSelection: [],
      formData
    }
  },
  created() {
    this.getDataList()
    this.findRoleList()
  },
  methods: {
    handleSearch() {
      this.page = 1
      this.getDataList()
    },
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    operateAction(scene, row) {
      switch (scene) {
        case 'create':
          this.$router.push({name: 'Update', query: {formData: encodeURIComponent(JSON.stringify(this.formData)), currentRouteName: this.$route.name }})
          break;
        case 'update':
          this.$router.push({name: 'Update', query: {rowId: row.id, formData: encodeURIComponent(JSON.stringify(this.formData)), currentRouteName: this.$route.name}})
          break;
        case 'detail':
          this.$router.push({name: 'Detail', query: {rowId: row.id, formData: encodeURIComponent(JSON.stringify(this.formData)), currentRouteName: this.$route.name}})
          break;
        case 'resetPassword':
          this.$refs.resetPwd.show(row, 'resetPassword')
          break;
        case 'delete':
          if(this.multipleSelection.length<=0){
            this.$message({
              message: '请选择操作行',
              type: 'info'
            })
          }else{
            var ids = this.multipleSelection.map(function (item){
              return item.id
            })
            deleteAdmins({
              obj_ids: ids
            }).then(res => {
              if (res.data.code == 10000) {
                this.getDataList()
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
              }
            })
          }
          break;
        default:
          break;
      }
    },
    //数据请求-----------------------------------------------
    // 获取列表数据
    getDataList() {
      this.searchParams.page = this.page
      this.searchParams.size = this.size
      findAdminList(this.searchParams).then(res => {
        if (res.data.code == 10000) {
          this.totalSize = res.data.data.total_count
          this.dataList = res.data.data.list
        }
      });
    },
    findRoleList() {
      findRoleList().then(res => {
        if (res.data.code == 10000) {
          var _list = res.data.data.list
          this.formData.options.role = _list
        }
      });
    },
  },
}
</script>


<style lang="scss" scoped></style>
